package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class VideoServiceImpl implements VideoService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RedisIdService redisIdService;

    @Override
    public PageBeanVo findRecommendVideoByPage(Long userId, Integer pageNum, Integer pageSize) {
        //1.创建查询对象(uid,排序,分页)
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //2.查询视频推荐表,返回推荐集合(视频id)
        List<RecommendVideo> recommendVideos = mongoTemplate.find(query, RecommendVideo.class);

        //3.遍历推荐表,获取每个视频详情,放入视频详情集合中
        List<Video> videoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(recommendVideos)) {
            recommendVideos.forEach(e->{
                Video video = mongoTemplate.findById(e.getVideoId(), Video.class);
                videoList.add(video);
            });
        }
        //4.查询总条数
        query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, RecommendVideo.class);

        //5.封装pageBeanVo返回
        return new PageBeanVo(pageNum,pageSize,count,videoList);
    }

    @Override
    public void save(Video video) {
        //1.给video设置vid(大数据)
        video.setVid(redisIdService.getNextId(ConstantUtil.VIDEO_ID));
        //2.保存视频对象
        mongoTemplate.save(video);

        //3.app上没有查看我的视频的操作,所以将视频推荐给我,将video封装recommendVideo对象,将其保存
        RecommendVideo recommendVideo = new RecommendVideo();
        recommendVideo.setDate(video.getCreated());
        recommendVideo.setVid(video.getVid());
        recommendVideo.setUserId(video.getUserId());//推荐给自己
        recommendVideo.setVideoId(video.getId());
        recommendVideo.setScore(99.99D);

        mongoTemplate.save(recommendVideo);
    }

    /**
     * 分页查询视频评论列表
     *
     * @param publishId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBeanVo findVideoCommentsByPage(ObjectId publishId, Integer pageNum, Integer pageSize) {
        Query query = new Query(
                Criteria.where("commentType").is(5).and("publishId").is(publishId)
        ).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum-1)*pageSize).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        query = new Query(
                Criteria.where("commentType").is(5).and("publishId").is(publishId)
        );

        long count = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNum,pageSize,count,commentList);
    }

    @Override
    public Video findById(ObjectId publishId) {
        return mongoTemplate.findById(publishId,Video.class);
    }



}
